package org.yoki.tool.core.helper;

import org.yoki.tool.core.config.DatabaseConfig;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * Created by SKY on 2017/6/26.
 */
public class JDBCConnectHelper {

    private static Connection connection ;

    private DatabaseConfig config ;

    public JDBCConnectHelper(DatabaseConfig config){
        this.config = config ;
    }

    public Connection getConnection(){
        if(null == connection){
            synchronized (Connection.class){
                if(null == connection) {
                    connection = buildConnection();
                }
            }
        }
        return connection ;
    }

    private Connection buildConnection(){
        Connection connection = null ;
        try {
            Class.forName(config.getDriver());
            connection = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPassword());
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection ;
    }

}
